package xfuzzy.alg_genetico.indeterminado.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.JTabbedPane;

import xfuzzy.alg_genetico.indeterminado.output.ConfigFile;
import xfuzzy.xfghl.algorithm.GeneticAlgorithm;
import xfuzzy.xfghl.cross.GPCrossover;
import xfuzzy.xfghl.evaluation.EvaluationXfuzzy;
import xfuzzy.xfghl.model.XfghlConfig;
import xfuzzy.xfghl.mutation.Mutation;
import xfuzzy.xfghl.output.GAOutput;
import xfuzzy.xfghl.population.RandomPopulationGenerator;
import xfuzzy.xfghl.replacement.GeneralizedCrowdingReplacement;
import xfuzzy.xfghl.replacement.Replacement;
import xfuzzy.xfghl.replacement.ReplacementRandom;
import xfuzzy.xfghl.replacement.ReplacementWorst;
import xfuzzy.xfghl.selection.Selection;
import xfuzzy.xfghl.selection.SelectionRandom;
import xfuzzy.xfghl.selection.SelectionTournament;
import xfuzzy.xfghl.stop.NumeroEvaluaciones;
import xfuzzy.xfghl.stop.StopCondition;
import xfuzzy.xfghl.test.finaltest.Seeds;

import javax.swing.SwingConstants;
import javax.swing.JTable;
import javax.swing.JScrollPane;

@SuppressWarnings("serial")
public class VistaAlgoritmo extends JFrame implements ViewDefinitions {

	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private JPanel optionsPanel;
	private JPanel mutationPanel;
	private JLabel lblPopulationSize;
	private JSpinner spinnerPopulationSize;
	//private JLabel lblCrossProbability;
	//private JSpinner spinnerCrossProbability;
	private JLabel lblMutationProbability;
	private JSpinner spinnerMutationProbability;
	private JLabel lblMutationSeed;
	private JTextField textFieldMutationSeed;
	private JPanel crossPanel;
	private JLabel lblCrossType;
	private JComboBox comboBoxCrossType;
	private JLabel lblCrossSeed;
	private JTextField textFieldCrossSeed;
	private JPanel selectionPanel;
	private JLabel lblSelectionType;
	private JComboBox comboBoxSelectionType;
	private JLabel lblTournamentSize;
	private JSpinner spinnerTournamentSizeSelection;
	private JLabel lblSelectionSeed;
	private JTextField textFieldSelectionSeed;
	private JPanel stopPanel;
	private JLabel lblStopCondition;
	private JComboBox comboBoxStopCondition;
	private JLabel lblNumberOfEvaluations;
	private JSpinner spinnerNumberOfEvaluations;
	private JLabel lblGenerationsWithoutMejoria;
	private JSpinner spinnerGenerationsWithoutImprovement;
	private JLabel lblOptimusValue;
	private JTextField textFieldOptimusValue;
	private JLabel lblNumberOfGenerations;
	private JSpinner spinnerNumberOfGenerations;
	private JLabel lblTimeLimit;
	private JSpinner spinnerTimeLimitMinutes;
	private JLabel lblTimeSeparator1;
	private JSpinner spinnerTimeLimitHours;
	private JPanel evaluationPanel;
	private JButton btnExecute;
	private JPanel graphicsPanel;
	private JLabel lblEvaluationType;
	private JComboBox comboBoxEvaluationType;
	private JPanel populationPanel;
	private JLabel lblGenerationType;
	private JComboBox comboBoxGenerationType;
	private JLabel lblPopulationSeed;
	private JTextField textFieldPopulationSeed;
	private JPanel replacementPanel;
	private JLabel lblReplacementType;
	private JComboBox comboBoxReplacementType;
	private JLabel lblReplacementSeed;
	private JTextField textFieldReplacementSeed;
	private JLabel lblTournamentSizeReplacement;
	private JSpinner spinnerTournamentSizeReplacement;
	private JPanel reparationPanel;
	private JButton btnLoad;
	private JButton btnExit;
	private JButton btnSave;
	private JLabel lblNotification;
	private JLabel lblReparationSeed;
	private JTextField textFieldReparationSeed;
	private JPanel resultsPanel;
	private JPanel generalResultPanel;
	private JLabel lblBestConfigurationResultPanel;
	private JLabel lblBestConfigurationResult;
	private JLabel lblFitnessResultPanel;
	private JLabel lblFitnessResult;
	private JLabel lblNumberOfGenerationsResultPanel;
	private JLabel lblNumberOfGenerationsResult;
	private JLabel lblNumberOfEvaluationsResultPanel;
	private JLabel lblNumberOfEvaluationsResult;
	private JLabel lblExecutionTimeResultPanel;
	private JLabel lblExecutionTimeResult;
	private JPanel dataResultPanel;
	private JPanel graphicResultPanel;
	private JLabel lblGraphicSchemaResult;
	private JTable tableDataResult;
	private JScrollPane scrollPaneDataResult;
	private DefaultTableModel modelData;
	private XfghlConfig config;
	private ConfigFile configFile;
	private GeneticAlgorithm algorithm;
	private StopCondition condp;
	private Selection sel;
	private GPCrossover cross;
	private EvaluationXfuzzy eval;
	private Replacement reempl;

	/**
	 * Create the frame.
	 */
	public VistaAlgoritmo() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1020, 601);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);
		
		graphicsPanel = new JPanel();
		tabbedPane.addTab("Graphics", null, graphicsPanel, null);
		graphicsPanel.setLayout(null);
		
		resultsPanel = new JPanel();
		tabbedPane.addTab("Results", null, resultsPanel, null);
		resultsPanel.setLayout(null);
		
		generalResultPanel = new JPanel();
		generalResultPanel.setBounds(12, 12, 424, 169);
		generalResultPanel.setBorder(BorderFactory.createTitledBorder("General results"));
		resultsPanel.add(generalResultPanel);
		generalResultPanel.setLayout(null);
		
		lblBestConfigurationResultPanel = new JLabel("Best configuration");
		lblBestConfigurationResultPanel.setBounds(12, 30, 140, 15);
		generalResultPanel.add(lblBestConfigurationResultPanel);
		
		lblFitnessResultPanel = new JLabel("Fitness");
		lblFitnessResultPanel.setBounds(12, 57, 70, 15);
		generalResultPanel.add(lblFitnessResultPanel);
		
		lblNumberOfGenerationsResultPanel = new JLabel("Number of generations");
		lblNumberOfGenerationsResultPanel.setBounds(12, 84, 170, 15);
		generalResultPanel.add(lblNumberOfGenerationsResultPanel);
		
		lblNumberOfEvaluationsResultPanel = new JLabel("Number of evaluations");
		lblNumberOfEvaluationsResultPanel.setBounds(12, 111, 170, 15);
		generalResultPanel.add(lblNumberOfEvaluationsResultPanel);
		
		lblExecutionTimeResultPanel = new JLabel("Execution time");
		lblExecutionTimeResultPanel.setBounds(12, 138, 140, 15);
		generalResultPanel.add(lblExecutionTimeResultPanel);
		
		lblBestConfigurationResult = new JLabel("");
		lblBestConfigurationResult.setHorizontalAlignment(SwingConstants.RIGHT);
		lblBestConfigurationResult.setBounds(262, 30, 150, 15);
		generalResultPanel.add(lblBestConfigurationResult);
		
		lblFitnessResult = new JLabel("");
		lblFitnessResult.setHorizontalAlignment(SwingConstants.RIGHT);
		lblFitnessResult.setBounds(232, 57, 180, 15);
		generalResultPanel.add(lblFitnessResult);
		
		lblNumberOfGenerationsResult = new JLabel("");
		lblNumberOfGenerationsResult.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNumberOfGenerationsResult.setBounds(322, 84, 90, 15);
		generalResultPanel.add(lblNumberOfGenerationsResult);
		
		lblNumberOfEvaluationsResult = new JLabel("");
		lblNumberOfEvaluationsResult.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNumberOfEvaluationsResult.setBounds(322, 111, 90, 15);
		generalResultPanel.add(lblNumberOfEvaluationsResult);
		
		lblExecutionTimeResult = new JLabel("");
		lblExecutionTimeResult.setHorizontalAlignment(SwingConstants.RIGHT);
		lblExecutionTimeResult.setBounds(272, 138, 140, 15);
		generalResultPanel.add(lblExecutionTimeResult);
		
		dataResultPanel = new JPanel();
		dataResultPanel.setBounds(12, 193, 424, 287);
		dataResultPanel.setBorder(BorderFactory.createTitledBorder("Data"));
		resultsPanel.add(dataResultPanel);
		dataResultPanel.setLayout(null);
		
		scrollPaneDataResult = new JScrollPane();
		scrollPaneDataResult.setBounds(12, 31, 400, 244);
		dataResultPanel.add(scrollPaneDataResult);
		
		String columnNames[] = { "Generation", "Fitness" };
		String dataValues[][] = { { "", "" } };
		modelData = new DefaultTableModel(dataValues, columnNames);
		modelData.setNumRows(0);
		tableDataResult = new JTable(modelData);
		tableDataResult.setRowSelectionAllowed(false);
		tableDataResult.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		TableColumn col = tableDataResult.getColumnModel().getColumn(0);
		col.setPreferredWidth(130);
		col = tableDataResult.getColumnModel().getColumn(1);
		col.setPreferredWidth(250);
		DefaultTableCellRenderer centerrenderer = new DefaultTableCellRenderer();
		centerrenderer.setHorizontalAlignment(JLabel.CENTER);
		tableDataResult.getColumnModel().getColumn(0).setCellRenderer(centerrenderer);
		DefaultTableCellRenderer rightrenderer = new DefaultTableCellRenderer();
		rightrenderer.setHorizontalAlignment(JLabel.RIGHT);
		tableDataResult.getColumnModel().getColumn(1).setCellRenderer(rightrenderer);
		scrollPaneDataResult.setViewportView(tableDataResult);
			
		graphicResultPanel = new JPanel();
		graphicResultPanel.setBounds(448, 12, 543, 510);
		graphicResultPanel.setBorder(BorderFactory.createTitledBorder("Graphic schema"));
		resultsPanel.add(graphicResultPanel);
		
		lblGraphicSchemaResult = new JLabel("");
		graphicResultPanel.add(lblGraphicSchemaResult);
		
		JButton btnLoadSchema = new JButton("Load schema");
		btnLoadSchema.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
//				BufferedImage schema = null;
//				try {
//					schema = ImageIO.read(new File("best.png"));
//				} catch (IOException e) {
//					e.printStackTrace();
//					System.out.println(e.getMessage());
//				}
				//JLabel picLabel = new JLabel(new ImageIcon(schema));
//				lblGraphicSchemaResult = new JLabel(new ImageIcon(schema));
				ImageIcon icon = new ImageIcon("best.png"); 
			    lblGraphicSchemaResult.setIcon(icon); 
//				graphicResultPanel.add(lblGraphicSchemaResult);
//				graphicResultPanel.repaint();
			}
		});
		btnLoadSchema.setBounds(12, 492, 140, 25);
		resultsPanel.add(btnLoadSchema);
		
		JButton btnExportData = new JButton("Export data");
		btnExportData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				List<Double> data = algorithm.getData();
				Double d = null;
				GAOutput.writeln(String.valueOf(algorithm.getResult().getNumGenerations()));
				GAOutput.writeln(String.valueOf(algorithm.getResult().getTime()));
				//GAOutput.writeln(String.valueOf(config.getCrossProbability()));
				GAOutput.writeln(String.valueOf(config.getMutationProbability()));
				GAOutput.writeln(String.valueOf(((SelectionTournament) sel).getNumIndividuos())); 
				for (int index = 0; index < data.size(); index++)  {
					 d = data.get(index);
					 GAOutput.writeln(String.valueOf(index + 1) + "; " + String.valueOf(d));
				}
			}
		});
		btnExportData.setBounds(164, 492, 117, 25);
		resultsPanel.add(btnExportData);
		
		optionsPanel = new JPanel();
		tabbedPane.addTab("Options", null, optionsPanel, null);
		optionsPanel.setLayout(null);
		
		mutationPanel = new JPanel();
		mutationPanel.setBounds(12, 401, 481, 90);
		mutationPanel.setBorder(BorderFactory.createTitledBorder("Mutation options"));
		optionsPanel.add(mutationPanel);
		mutationPanel.setLayout(null);
		
		lblMutationProbability = new JLabel("Mutation probability");
		lblMutationProbability.setBounds(12, 30, 170, 15);
		mutationPanel.add(lblMutationProbability);
		
		spinnerMutationProbability = new JSpinner();
		spinnerMutationProbability.setModel(new SpinnerNumberModel(0.01, 0.00, 1.00, 0.01));
		spinnerMutationProbability.setBounds(419, 32, 50, 20);
		mutationPanel.add(spinnerMutationProbability);
		
		lblMutationSeed = new JLabel("Mutation seed");
		lblMutationSeed.setBounds(12, 57, 110, 15);
		mutationPanel.add(lblMutationSeed);
		
		textFieldMutationSeed = new JTextField();
		textFieldMutationSeed.setBounds(355, 59, 114, 19);
		mutationPanel.add(textFieldMutationSeed);
		textFieldMutationSeed.setColumns(10);
		
		crossPanel = new JPanel();
		crossPanel.setBounds(12, 12, 481, 120);
		crossPanel.setBorder(BorderFactory.createTitledBorder("Cross options"));
		optionsPanel.add(crossPanel);
		crossPanel.setLayout(null);
		
		lblCrossType = new JLabel("Cross type");
		lblCrossType.setBounds(12, 30, 90, 15);
		crossPanel.add(lblCrossType);
		
		comboBoxCrossType = new JComboBox();
		comboBoxCrossType.setBounds(289, 22, 180, 20);
		//comboBoxCrossType.addItem(onePointCrossType);
		//comboBoxCrossType.addItem(twoPointsCrossType);
		//comboBoxCrossType.addItem(orderCrossType);
		comboBoxCrossType.addItem(geneticProgrammingCrossType);
		comboBoxCrossType.setSelectedIndex(-1);
		crossPanel.add(comboBoxCrossType);
		
		lblCrossSeed = new JLabel("Seed");
		lblCrossSeed.setBounds(12, 57, 70, 15);
		crossPanel.add(lblCrossSeed);
		
		textFieldCrossSeed = new JTextField();
		textFieldCrossSeed.setBounds(355, 50, 114, 19);
		crossPanel.add(textFieldCrossSeed);
		textFieldCrossSeed.setColumns(10);
		
//		lblCrossProbability = new JLabel("Cross probability");
//		lblCrossProbability.setBounds(12, 84, 170, 15);
//		crossPanel.add(lblCrossProbability);
//		
//		spinnerCrossProbability = new JSpinner();
//		spinnerCrossProbability.setBounds(419, 79, 50, 20);
//		crossPanel.add(spinnerCrossProbability);
//		spinnerCrossProbability.setModel(new SpinnerNumberModel(0.01, 0.00, 1.00, 0.01));
		
		selectionPanel = new JPanel();
		selectionPanel.setBounds(12, 144, 481, 120);
		selectionPanel.setBorder(BorderFactory.createTitledBorder("Selection options"));
		optionsPanel.add(selectionPanel);
		selectionPanel.setLayout(null);
		
		lblSelectionType = new JLabel("Selection type");
		lblSelectionType.setBounds(12, 30, 110, 15);
		selectionPanel.add(lblSelectionType);
		
		comboBoxSelectionType = new JComboBox();
		comboBoxSelectionType.setBounds(289, 34, 180, 20);
		comboBoxSelectionType.addItem(tournamentSelection);
		comboBoxSelectionType.addItem(randomSelection);
		comboBoxSelectionType.setSelectedIndex(-1);
		comboBoxSelectionType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String typeSelected = comboBoxSelectionType.getSelectedItem().toString();
				if (typeSelected.compareTo(tournamentSelection) == 0)  {
					lblTournamentSize.setEnabled(true);
					spinnerTournamentSizeSelection.setEnabled(true);
				}
				else  {
					lblTournamentSize.setEnabled(false);
					spinnerTournamentSizeSelection.setEnabled(false);
				}
			}
		});
		selectionPanel.add(comboBoxSelectionType);
		
		lblTournamentSize = new JLabel("Tournament size");
		lblTournamentSize.setBounds(12, 57, 140, 15);
		lblTournamentSize.setEnabled(false);
		selectionPanel.add(lblTournamentSize);
		
		spinnerTournamentSizeSelection = new JSpinner();
		spinnerTournamentSizeSelection.setBounds(424, 62, 45, 20);
		spinnerTournamentSizeSelection.setEnabled(false);
		spinnerTournamentSizeSelection.setModel(new SpinnerNumberModel(new Integer(3), new Integer(1), null, new Integer(1)));
		selectionPanel.add(spinnerTournamentSizeSelection);
		
		lblSelectionSeed = new JLabel("Seed");
		lblSelectionSeed.setBounds(12, 84, 70, 15);
		selectionPanel.add(lblSelectionSeed);
		
		textFieldSelectionSeed = new JTextField();
		textFieldSelectionSeed.setBounds(355, 89, 114, 19);
		selectionPanel.add(textFieldSelectionSeed);
		textFieldSelectionSeed.setColumns(10);
		
		stopPanel = new JPanel();
		stopPanel.setBounds(510, 144, 481, 199);
		stopPanel.setBorder(BorderFactory.createTitledBorder("Stop options"));
		optionsPanel.add(stopPanel);
		stopPanel.setLayout(null);
		
		lblStopCondition = new JLabel("Stop condition");
		lblStopCondition.setBounds(12, 30, 120, 15);
		stopPanel.add(lblStopCondition);
		
		comboBoxStopCondition = new JComboBox();
		comboBoxStopCondition.setBounds(219, 22, 250, 20);
		comboBoxStopCondition.addItem(numberOfGenerationsStopCondition);
		comboBoxStopCondition.addItem(generationsWithoutImprovementStopCondition);
		comboBoxStopCondition.addItem(numberOfEvaluationsStopCondition);
		comboBoxStopCondition.addItem(optimusValueStopCondition);
		comboBoxStopCondition.addItem(timeLimitStopCondition);
		comboBoxStopCondition.setSelectedIndex(-1);
		comboBoxStopCondition.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				
				lblGenerationsWithoutMejoria.setEnabled(false);
				spinnerGenerationsWithoutImprovement.setEnabled(false);
				lblNumberOfEvaluations.setEnabled(false);
				spinnerNumberOfEvaluations.setEnabled(false);
				lblOptimusValue.setEnabled(false);
				textFieldOptimusValue.setEnabled(false);
				lblNumberOfGenerations.setEnabled(false);
				spinnerNumberOfGenerations.setEnabled(false);
				lblTimeLimit.setEnabled(false);
				spinnerTimeLimitMinutes.setEnabled(false);
				spinnerTimeLimitHours.setEnabled(false);
				lblTimeSeparator1.setEnabled(false);
				String conditionSelected = comboBoxStopCondition.getSelectedItem().toString();
				if (conditionSelected.compareTo(generationsWithoutImprovementStopCondition) == 0)  {
					lblGenerationsWithoutMejoria.setEnabled(true);
					spinnerGenerationsWithoutImprovement.setEnabled(true);
				}
				
				if (conditionSelected.compareTo(numberOfEvaluationsStopCondition) == 0)  {
					lblNumberOfEvaluations.setEnabled(true);
					spinnerNumberOfEvaluations.setEnabled(true);
				}
				
				if (conditionSelected.compareTo(optimusValueStopCondition) == 0)  {
					lblOptimusValue.setEnabled(true);
					textFieldOptimusValue.setEnabled(true);
				}
				
				if (conditionSelected.compareTo(numberOfGenerationsStopCondition) == 0)  {
					lblNumberOfGenerations.setEnabled(true);
					spinnerNumberOfGenerations.setEnabled(true);
				}
				
				if (conditionSelected.compareTo(timeLimitStopCondition) == 0)  {
					lblTimeLimit.setEnabled(true);
					spinnerTimeLimitMinutes.setEnabled(true);
					spinnerTimeLimitHours.setEnabled(true);
					lblTimeSeparator1.setEnabled(true);
				}
			}
		});
		stopPanel.add(comboBoxStopCondition);
		
		lblNumberOfEvaluations = new JLabel("Number of evaluations");
		lblNumberOfEvaluations.setBounds(12, 57, 170, 15);
		lblNumberOfEvaluations.setEnabled(false);
		stopPanel.add(lblNumberOfEvaluations);
		
		spinnerNumberOfEvaluations = new JSpinner();
		spinnerNumberOfEvaluations.setBounds(419, 50, 50, 20);
		spinnerNumberOfEvaluations.setModel(new SpinnerNumberModel(new Integer(30), new Integer(1), null, new Integer(1)));
		spinnerNumberOfEvaluations.setEnabled(false);
		stopPanel.add(spinnerNumberOfEvaluations);
		
		lblGenerationsWithoutMejoria = new JLabel("Iterations without improvement");
		lblGenerationsWithoutMejoria.setBounds(12, 84, 230, 15);
		lblGenerationsWithoutMejoria.setEnabled(false);
		stopPanel.add(lblGenerationsWithoutMejoria);
		
		spinnerGenerationsWithoutImprovement = new JSpinner();
		spinnerGenerationsWithoutImprovement.setBounds(419, 77, 50, 20);
		spinnerGenerationsWithoutImprovement.setModel(new SpinnerNumberModel(new Integer(10), new Integer(1), null, new Integer(1)));
		spinnerGenerationsWithoutImprovement.setEnabled(false);
		stopPanel.add(spinnerGenerationsWithoutImprovement);
		
		lblOptimusValue = new JLabel("Optimus value");
		lblOptimusValue.setBounds(12, 111, 120, 15);
		lblOptimusValue.setEnabled(false);
		stopPanel.add(lblOptimusValue);
		
		textFieldOptimusValue = new JTextField();
		textFieldOptimusValue.setBounds(329, 104, 140, 19);
		stopPanel.add(textFieldOptimusValue);
		textFieldOptimusValue.setEnabled(false);
		textFieldOptimusValue.setColumns(10);
		
		lblNumberOfGenerations = new JLabel("Number of generations");
		lblNumberOfGenerations.setBounds(12, 138, 170, 15);
		lblNumberOfGenerations.setEnabled(false);
		stopPanel.add(lblNumberOfGenerations);
		
		spinnerNumberOfGenerations = new JSpinner();
		spinnerNumberOfGenerations.setBounds(429, 133, 40, 20);
		spinnerNumberOfGenerations.setModel(new SpinnerNumberModel(new Integer(20), new Integer(1), null, new Integer(1)));
		spinnerNumberOfGenerations.setEnabled(false);
		stopPanel.add(spinnerNumberOfGenerations);
		
		lblTimeLimit = new JLabel("Time limit (HH:MM)");
		lblTimeLimit.setBounds(12, 165, 150, 15);
		lblTimeLimit.setEnabled(false);
		stopPanel.add(lblTimeLimit);
		
		spinnerTimeLimitMinutes = new JSpinner();
		spinnerTimeLimitMinutes.setBounds(429, 160, 40, 20);
		spinnerTimeLimitMinutes.setModel(new SpinnerNumberModel(new Integer(1), new Integer(1), new Integer(60), new Integer(1)));
		spinnerTimeLimitMinutes.setEnabled(false);
		stopPanel.add(spinnerTimeLimitMinutes);
		
		lblTimeSeparator1 = new JLabel(":");
		lblTimeSeparator1.setBounds(406, 162, 5, 15);
		lblTimeSeparator1.setEnabled(false);
		stopPanel.add(lblTimeSeparator1);
		
		spinnerTimeLimitHours = new JSpinner();
		spinnerTimeLimitHours.setBounds(348, 160, 40, 20);
		spinnerTimeLimitHours.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		spinnerTimeLimitHours.setEnabled(false);
		stopPanel.add(spinnerTimeLimitHours);
		
		btnExecute = new JButton("Execute");
		btnExecute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				config = new XfghlConfig();
				long randomSeed = 0;
				// value = spinnerCrossProbability.getValue().toString();
				//config.setCrossProbability(Double.valueOf(value));
				 String value = spinnerMutationProbability.getValue().toString();
				config.setMutationProbability(Double.valueOf(value));
				value = spinnerPopulationSize.getValue().toString();
				config.setPopulationSize(Integer.valueOf(value));
				
				condp = null;
				String conditionSelected = comboBoxStopCondition.getSelectedItem().toString();
//				if (conditionSelected.compareTo(generationsWithoutImprovementStopCondition) == 0)  {
//					int iterations = Integer.valueOf(spinnerGenerationsWithoutImprovement.getValue().toString());
//					condp = new GenerationsWithoutImprovement(iterations);
//				}
				
//				if (conditionSelected.compareTo(timeLimitStopCondition) == 0)  {
//					long minutes = Long.valueOf(spinnerTimeLimitMinutes.getValue().toString()) * 60 * 1000;
//					long hours = Long.valueOf(spinnerTimeLimitHours.getValue().toString()) * 60 * 60 * 1000;
//					condp = new TimeLimit(minutes + hours); 
//				}
				
				if (conditionSelected.compareTo(numberOfEvaluationsStopCondition) == 0)  {
					int evaluations = Integer.valueOf(spinnerNumberOfEvaluations.getValue().toString());
					condp = new NumeroEvaluaciones(evaluations);
				}
				
//				if (conditionSelected.compareTo(optimusValueStopCondition) == 0)  {
//					double optimusValue = Double.valueOf(textFieldOptimusValue.getText());
//					condp = new OptimusValue(optimusValue);
//				}
				
//				if (conditionSelected.compareTo(numberOfGenerationsStopCondition) == 0)  {
//					int numberGenerations = Integer.valueOf(spinnerNumberOfGenerations.getValue().toString());
//					condp = new NumberGenerations(numberGenerations);
//				}
				
				sel = null;
				randomSeed = Long.valueOf(textFieldSelectionSeed.getText());
				String selectionSelected = comboBoxSelectionType.getSelectedItem().toString();
				if (selectionSelected.compareTo(randomSelection) == 0)  {
					sel = new SelectionRandom(randomSeed);
				}
				
				if (selectionSelected.compareTo(tournamentSelection) == 0)  {
					int sizeTournament = Integer.valueOf(spinnerTournamentSizeSelection.getValue().toString());
					sel = new SelectionTournament(sizeTournament, randomSeed);
				}
				
				cross = null;
				randomSeed = Long.valueOf(textFieldCrossSeed.getText());
				String crossSelected = comboBoxCrossType.getSelectedItem().toString();
//				if (crossSelected.compareTo(onePointCrossType) == 0)  {
//					cross = new CruceUnPunto(randomSeed);
//				}
//				
//				if (crossSelected.compareTo(twoPointsCrossType) == 0)  {
//					cross = new CruceDosPuntos(randomSeed);
//				}
//				
//				if (crossSelected.compareTo(orderCrossType) == 0)  {
//					cross = new CruceOrden(randomSeed);
//				}
				
				if (crossSelected.compareTo(geneticProgrammingCrossType) == 0)  {
					cross = new GPCrossover(randomSeed);
				}
				
				randomSeed = Long.valueOf(textFieldMutationSeed.getText());
				Mutation mut = new Mutation(randomSeed);
				
				eval = null;
			//	String evaluationSelected = comboBoxEvaluationType.getSelectedItem().toString();
//				if (evaluationSelected.compareTo(fileEvaluation) == 0) 
//					eval = new EvaluationFile(config.getnumVarEnt());
				
//				if (evaluationSelected.compareTo(xfuzzyEvaluation) == 0)
//					eval = new EvaluationXfuzzy();
				
				reempl = null;
				String selectedReplacement = comboBoxReplacementType.getSelectedItem().toString();
				if (selectedReplacement.compareTo(deterministicCrowding) == 0)
					reempl = new GeneralizedCrowdingReplacement(1, Seeds.seedDeterministicCrowding1);
				
//				if (selectedReplacement.compareTo(WAMSReplacement) == 0)
//					reempl = new PeorEntreSemejantes();
				
				if (selectedReplacement.compareTo(randomReplacement) == 0)  {
					long replacementSeed = Long.valueOf(textFieldReplacementSeed.getText());
					reempl = new ReplacementRandom(replacementSeed);
				}
					
				if (selectedReplacement.compareTo(worstReplacement) == 0)
					reempl = new ReplacementWorst();
				
//				if (selectedReplacement.compareTo(tournamentReplacement) == 0)  {
//					long replacementSeed = Long.valueOf(textFieldReplacementSeed.getText());
//					int tournamentSizeReplacement = Integer.valueOf(spinnerTournamentSizeReplacement.getValue().toString());
//					reempl = new TorneoRestringido(replacementSeed, tournamentSizeReplacement);
//				}
				
				RandomPopulationGenerator genPbl = null;
				String generationSelected = comboBoxGenerationType.getSelectedItem().toString();
				int populationSize = Integer.valueOf(spinnerPopulationSize.getValue().toString());
				if (generationSelected.compareTo(randomGeneration) == 0)  {
					long populationSeed = Long.valueOf(textFieldPopulationSeed.getText());
					genPbl = new RandomPopulationGenerator(populationSeed, populationSize);
				}
				
				algorithm = new GeneticAlgorithm(sel, cross, mut, reempl, genPbl, condp, eval, config);
				lblNotification.setText("Execution started");
				Thread gaThread = new Thread(algorithm);
				gaThread.run();
				try {
					gaThread.join();
				} 
				catch (InterruptedException e) {
					e.printStackTrace();
				}
				lblNotification.setText("Execution finished");
				lblBestConfigurationResult.setText(algorithm.getBest().getCodificacion());
				lblFitnessResult.setText(String.valueOf(algorithm.getBest().getFitness()));
				lblNumberOfEvaluationsResult.setText(String.valueOf(algorithm.getResult().getNumEvaluations()));
				lblNumberOfGenerationsResult.setText(String.valueOf(algorithm.getResult().getNumGenerations()));
				long time = algorithm.getResult().getTime();
				lblExecutionTimeResult.setText(String.valueOf(time) + " ms");
				//XfhlModuleToGraph.createGraph(algorithm.getBest(), "best", true);
				List<Double> data = algorithm.getData();
				modelData.setNumRows(0);
				int generationCounter = 1;
				for (Double valueFitness : data)  {
					 modelData.addRow(new Object [] { generationCounter, valueFitness });
					 generationCounter++;
				}
				tableDataResult.setModel(modelData);
			}
		});
		btnExecute.setBounds(12, 503, 102, 25);
		optionsPanel.add(btnExecute);
		
		evaluationPanel = new JPanel();
		evaluationPanel.setBounds(510, 355, 481, 62);
		evaluationPanel.setBorder(BorderFactory.createTitledBorder("Evaluation options"));
		optionsPanel.add(evaluationPanel);
		evaluationPanel.setLayout(null);
		
		lblEvaluationType = new JLabel("Evaluation type");
		lblEvaluationType.setBounds(12, 30, 110, 15);
		evaluationPanel.add(lblEvaluationType);
		
		comboBoxEvaluationType = new JComboBox();
		comboBoxEvaluationType.setBounds(289, 27, 180, 20);
		comboBoxEvaluationType.addItem(fileEvaluation);
		comboBoxEvaluationType.addItem(xfuzzyEvaluation);
		comboBoxEvaluationType.setSelectedIndex(-1);
		evaluationPanel.add(comboBoxEvaluationType);
		
		populationPanel = new JPanel();
		populationPanel.setBounds(12, 276, 481, 113);
		populationPanel.setBorder(BorderFactory.createTitledBorder("Population options"));
		optionsPanel.add(populationPanel);
		populationPanel.setLayout(null);
		
		lblGenerationType = new JLabel("Generation type");
		lblGenerationType.setBounds(12, 30, 121, 15);
		populationPanel.add(lblGenerationType);
		
		comboBoxGenerationType = new JComboBox();
		comboBoxGenerationType.setBounds(289, 24, 180, 20);
		comboBoxGenerationType.addItem(randomGeneration);
		comboBoxGenerationType.setSelectedIndex(-1);
		populationPanel.add(comboBoxGenerationType);
		
		lblPopulationSeed = new JLabel("Seed");
		lblPopulationSeed.setBounds(12, 57, 70, 15);
		populationPanel.add(lblPopulationSeed);
		
		textFieldPopulationSeed = new JTextField();
		textFieldPopulationSeed.setBounds(355, 52, 114, 19);
		populationPanel.add(textFieldPopulationSeed);
		textFieldPopulationSeed.setColumns(10);
		
		lblPopulationSize = new JLabel("Population size");
		lblPopulationSize.setBounds(12, 84, 170, 15);
		populationPanel.add(lblPopulationSize);
		
		spinnerPopulationSize = new JSpinner();
		spinnerPopulationSize.setBounds(419, 79, 50, 20);
		populationPanel.add(spinnerPopulationSize);
		spinnerPopulationSize.setModel(new SpinnerNumberModel(new Integer(30), new Integer(5), null, new Integer(1)));
		
		replacementPanel = new JPanel();
		replacementPanel.setBounds(510, 12, 481, 120);
		replacementPanel.setBorder(BorderFactory.createTitledBorder("Replacement options"));
		optionsPanel.add(replacementPanel);
		replacementPanel.setLayout(null);
		
		lblReplacementType = new JLabel("Replacement type");
		lblReplacementType.setBounds(12, 30, 140, 15);
		replacementPanel.add(lblReplacementType);
		
		comboBoxReplacementType = new JComboBox();
		comboBoxReplacementType.setBounds(289, 22, 180, 20);
		comboBoxReplacementType.addItem(tournamentReplacement);
		comboBoxReplacementType.addItem(worstReplacement);
		comboBoxReplacementType.addItem(randomReplacement);
		comboBoxReplacementType.addItem(WAMSReplacement);
		comboBoxReplacementType.addItem(deterministicCrowding);
		comboBoxReplacementType.setSelectedIndex(-1);
		comboBoxReplacementType.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				lblReplacementSeed.setEnabled(false);
				textFieldReplacementSeed.setEnabled(false);
				lblTournamentSizeReplacement.setEnabled(false);
				spinnerTournamentSizeReplacement.setEnabled(false);
				String replacementSelected = comboBoxReplacementType.getSelectedItem().toString();
				if (replacementSelected.compareTo(tournamentReplacement) == 0)  {
					lblReplacementSeed.setEnabled(true);
					textFieldReplacementSeed.setEnabled(true);
					lblTournamentSizeReplacement.setEnabled(true);
					spinnerTournamentSizeReplacement.setEnabled(true);
				}
				
				if (replacementSelected.compareTo(randomReplacement) == 0)  {
					lblReplacementSeed.setEnabled(true);
					textFieldReplacementSeed.setEnabled(true);
				}
			}
		});
		replacementPanel.add(comboBoxReplacementType);
		
		lblReplacementSeed = new JLabel("Seed");
		lblReplacementSeed.setBounds(12, 57, 70, 15);
		lblReplacementSeed.setEnabled(false);
		replacementPanel.add(lblReplacementSeed);
		
		textFieldReplacementSeed = new JTextField();
		textFieldReplacementSeed.setBounds(355, 52, 114, 19);
		textFieldReplacementSeed.setEnabled(false);
		replacementPanel.add(textFieldReplacementSeed);
		textFieldReplacementSeed.setColumns(10);
		
		lblTournamentSizeReplacement = new JLabel("Tournament size");
		lblTournamentSizeReplacement.setBounds(12, 84, 160, 15);
		lblTournamentSizeReplacement.setEnabled(false);
		replacementPanel.add(lblTournamentSizeReplacement);
		
		spinnerTournamentSizeReplacement = new JSpinner();
		spinnerTournamentSizeReplacement.setBounds(429, 79, 40, 20);
		spinnerTournamentSizeReplacement.setEnabled(false);
		replacementPanel.add(spinnerTournamentSizeReplacement);
		
		btnLoad = new JButton("Load");
		btnLoad.setBounds(126, 503, 117, 25);
		btnLoad.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
			
				configFile = new ConfigFile();
				configFile.load();
				// mutacion
				spinnerMutationProbability.setValue(configFile.getMutationProbability());
				textFieldMutationSeed.setText(String.valueOf(configFile.getMutationSeed()));
				// cruce
				comboBoxCrossType.setSelectedItem(configFile.getCrossSelected());
				//spinnerCrossProbability.setValue(configFile.getCrossProbability());
				textFieldCrossSeed.setText(String.valueOf(configFile.getCrossSeed()));
				// poblacion
				spinnerPopulationSize.setValue(configFile.getPopulationSize());
				// reparacion
				textFieldReparationSeed.setText(String.valueOf(configFile.getReparationSeed()));
				// condicion de parada
				comboBoxStopCondition.setSelectedItem(configFile.getStopConditionSelected());
				if (configFile.getStopConditionSelected().compareTo(generationsWithoutImprovementStopCondition) == 0)
					spinnerGenerationsWithoutImprovement.setValue(configFile.getGenerationsWithoutImprovement());
				if (configFile.getStopConditionSelected().compareTo(numberOfEvaluationsStopCondition) == 0)
					spinnerNumberOfEvaluations.setValue(configFile.getNumberEvaluations());
				if (configFile.getStopConditionSelected().compareTo(numberOfGenerationsStopCondition) == 0)
					spinnerNumberOfGenerations.setValue(configFile.getNumberGenerations());
				if (configFile.getStopConditionSelected().compareTo(timeLimitStopCondition) == 0)  {
					spinnerTimeLimitMinutes.setValue(configFile.getMinutesTimeLimit());
					spinnerTimeLimitHours.setValue(configFile.getHoursTimeLimit());
				}
				if (configFile.getStopConditionSelected().compareTo(optimusValueStopCondition) == 0)
					textFieldOptimusValue.setText(String.valueOf(configFile.getOptimusValue()));
				
				// seleccion
				comboBoxSelectionType.setSelectedItem(configFile.getSelectionSelected());
				textFieldSelectionSeed.setText(String.valueOf(configFile.getSelectionSeed()));
				if (configFile.getSelectionSelected().compareTo(tournamentSelection) == 0)
					spinnerTournamentSizeSelection.setValue(configFile.getTournamentSizeSelection());
				// evaluacion
				comboBoxEvaluationType.setSelectedItem(configFile.getEvaluationSelected());
				// reemplazo
				comboBoxReplacementType.setSelectedItem(configFile.getReplacementSelected());
				textFieldReplacementSeed.setText(String.valueOf(configFile.getReplacementSeed()));
				spinnerTournamentSizeReplacement.setValue(configFile.getTournamentSizeReplacement());
				// generacion
				comboBoxGenerationType.setSelectedItem(configFile.getGenerationSelected());
				textFieldPopulationSeed.setText(String.valueOf(configFile.getPopulationSeed()));
			}
		});
		optionsPanel.add(btnLoad);
		
		btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnExit.setBounds(384, 503, 117, 25);
		optionsPanel.add(btnExit);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(255, 503, 117, 25);
		btnSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {

				configFile = new ConfigFile();
				configFile.setPopulationSize(Integer.valueOf(spinnerPopulationSize.getValue().toString()));
				//configFile.setCrossProbability(Double.valueOf(spinnerCrossProbability.getValue().toString()));
				configFile.setMutationProbability(Double.valueOf(spinnerMutationProbability.getValue().toString()));
				configFile.setMutationSeed(Long.valueOf(textFieldMutationSeed.getText()));
				configFile.setCrossSelected(comboBoxCrossType.getSelectedItem().toString());
				configFile.setCrossSeed(Long.valueOf(textFieldCrossSeed.getText()));
				configFile.setSelectionSelected(comboBoxSelectionType.getSelectedItem().toString());
				configFile.setTournamentSizeSelection(Integer.valueOf(spinnerTournamentSizeSelection.getValue().toString()));
				configFile.setSelectionSeed(Long.valueOf(textFieldSelectionSeed.getText()));
				configFile.setStopConditionSelected(comboBoxStopCondition.getSelectedItem().toString());
				configFile.setNumberEvaluations(Integer.valueOf(spinnerNumberOfEvaluations.getValue().toString()));
				configFile.setGenerationsWithoutImprovement(Integer.valueOf(spinnerGenerationsWithoutImprovement.getValue().toString()));
				if (!textFieldOptimusValue.getText().isEmpty())
					configFile.setOptimusValue(Double.valueOf(textFieldOptimusValue.getText()));
				configFile.setNumberGenerations(Integer.valueOf(spinnerNumberOfGenerations.getValue().toString()));
				configFile.setMinutesTimeLimit(Integer.valueOf(spinnerTimeLimitMinutes.getValue().toString()));
				configFile.setHoursTimeLimit(Integer.valueOf(spinnerTimeLimitHours.getValue().toString()));
				configFile.setEvaluationSelected(comboBoxEvaluationType.getSelectedItem().toString());
				configFile.setGenerationSelected(comboBoxGenerationType.getSelectedItem().toString());
				configFile.setPopulationSeed(Long.valueOf(textFieldPopulationSeed.getText()));
				configFile.setReplacementSelected(comboBoxReplacementType.getSelectedItem().toString());
				if (!textFieldReplacementSeed.getText().isEmpty())
					configFile.setReplacementSeed(Long.valueOf(textFieldReplacementSeed.getText()));
				configFile.setTournamentSizeReplacement(Integer.valueOf(spinnerTournamentSizeReplacement.getValue().toString()));
				configFile.save();
			}
		});
		optionsPanel.add(btnSave);
		
		lblNotification = new JLabel("Notification");
		lblNotification.setBounds(519, 508, 271, 15);
		lblNotification.setText("");
		optionsPanel.add(lblNotification);
		
		reparationPanel = new JPanel();
		reparationPanel.setBounds(510, 429, 481, 62);
		reparationPanel.setBorder(BorderFactory.createTitledBorder("Reparation options"));
		optionsPanel.add(reparationPanel);
		reparationPanel.setLayout(null);
		
		lblReparationSeed = new JLabel("Reparation seed");
		lblReparationSeed.setBounds(12, 30, 125, 15);
		reparationPanel.add(lblReparationSeed);
		
		textFieldReparationSeed = new JTextField();
		textFieldReparationSeed.setBounds(355, 28, 114, 19);
		reparationPanel.add(textFieldReparationSeed);
		textFieldReparationSeed.setColumns(10);
		
		tabbedPane.setSelectedIndex(2);
	}
}
